package cn.stylefeng.guns.modular.shuheng.controller;


import cn.stylefeng.guns.core.common.constant.Constants;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.HtmlUtil;
import cn.stylefeng.guns.modular.shuheng.entity.Region;
import cn.stylefeng.guns.modular.shuheng.service.RegionService;
import cn.stylefeng.guns.modular.shuheng.warpper.RegionWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Date;

import org.springframework.stereotype.Controller;
import cn.stylefeng.roses.core.base.controller.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengpp
 * @since 2021-03-04
 */
@Controller
@RequestMapping("/region")
public class RegionController extends BaseController {

   private String PREFIX = "/modular/shuheng/region/";

   @Autowired
   private RegionService  regionService;

   @RequestMapping("")
   public String index(){
      return PREFIX + "region.html";
   }

   @RequestMapping("/list")
   @ResponseBody
   public Object list(@RequestParam(value = "regionName", required = false) String regionName){
      Page<Map<String,Object>> page = regionService.listRegion(regionName,null,null);
      page = new RegionWrapper(page).wrap();
      return LayuiPageFactory.createPageInfo(page);
   }

   @RequestMapping("/region_add")
   public String addView() {
      return PREFIX + "region_add.html";
   }

   @RequestMapping(value = "/add")
   @ResponseBody
   public ResponseData add(Region region) {
      if (region == null) {
         throw new RequestEmptyException();
      }
      //设置编码
      regionService.getRegionCode(region);
      this.regionService.save(region);
      return SUCCESS_TIP;
   }


   @RequestMapping("/region_edit")
   public String editView(@RequestParam Long regionId){
      if (ToolUtil.isEmpty(regionId)) {
         throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
      }
      //用来记录日志
      Region region = this.regionService.getById(regionId);
      LogObjectHolder.me().set(region);
      return PREFIX + "region_edit.html";
   }

   @RequestMapping(value = "/edit")
   @ResponseBody
   public ResponseData edit(Region region){
      if (region == null) {
         throw new RequestEmptyException();
      }
      this.regionService.updateById(region);
      return SUCCESS_TIP;
   }

   @RequestMapping(value = "/getRegionInfo")
   @ResponseBody
   public ResponseData getRegionInfo(@RequestParam Long regionId) {
      if (ToolUtil.isEmpty(regionId)) {
         throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
      }
      Region region = this.regionService.getById(regionId);
      return ResponseData.success(region);
   }


   /**
   * 删除
   * @param regionId
   * @return
   */
   @RequestMapping(value = "/delete")
   @ResponseBody
   public ResponseData delete(@RequestParam Long regionId) {
      if (ToolUtil.isEmpty(regionId)) {
         throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
      }
      this.regionService.removeById(regionId);
      return SUCCESS_TIP;
   }

   /**
    * 上级下拉选择
    * @param regionLevel
    * @return
    */
   @RequestMapping(value = "/selectParent")
   @ResponseBody
   public Object selectParent(@RequestParam(value = "regionLevel",required = false)String regionLevel){
      QueryWrapper<Region> wrapper = new QueryWrapper<Region>();
      if(Constants.STRING_THERE.equals(regionLevel)){
         //区县的上级.列出山东各地市
         wrapper.eq("region_level",Constants.STRING_TWO);
         wrapper.eq("parent_id","16");
      }
      if(Constants.STRING_TWO.equals(regionLevel)){
         //地市的上级，列出山东省
         wrapper.eq("region_level",Constants.STRING_ONE);
         wrapper.eq("parent_id",Constants.STRING_ONE);
      }
      List<Map<String,Object>> list = regionService.listMaps(wrapper);
      String options = HtmlUtil.listMap2HtmlOptions(list,"region_id","region_name");
      return options;

   }

   /**
    * 查询下级
    * @param regionLevel
    * @return
    */
   @RequestMapping(value = "/selectChildren")
   @ResponseBody
   public Object selectChildren(@RequestParam(value = "regionLevel",required = false)String regionLevel,@RequestParam(value = "parentId",required = false) String parentId){
      QueryWrapper<Region> wrapper = new QueryWrapper<Region>();
      if(Constants.STRING_THERE.equals(regionLevel)){
         //区县
         wrapper.eq("region_level",regionLevel);
         wrapper.eq("parent_id",parentId);
      }
      if(Constants.STRING_TWO.equals(regionLevel)){
         //地市的上级，列出山东省
         wrapper.eq("region_level",Constants.STRING_TWO);
         wrapper.eq("parent_id","16");
      }
      List<Map<String,Object>> list = regionService.listMaps(wrapper);
      String options = HtmlUtil.listMap2HtmlOptions(list,"region_id","region_name");
      return options;
   }
}
